﻿using DTCash.Logger;
using DTCash.Logger.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTCash.Redis;
using Uranus.Service.OrderService;

namespace Uranus.Service
{
    public class Order : BaseData
    {
        private static Order order = null;

        public static Order GetOrder()
        {
            if (order == null)
            {
                order = new Order();
            }
            return order;
        }
        /// <summary>
        /// 根据用户ID查询，获取用户是否有投资过新手标
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public OrderItemResponse GetUserNoviceOrderItemByUserID(int UserID)
        {
            OrderServiceClient orderServiceClient = new OrderServiceClient();
            try
            {
                Func<OrderItemResponse> method = delegate
                {
                    OrderItemResponse response = orderServiceClient.GetUserNoviceOrderItemByUserID(UserID);
                    return response.Code == 2 ? null : response;
                };
                return GetData<OrderItemResponse>(method,
                           CacheItem.InitByIdentity(UserID.ToString()
                               , "GetUserNoviceOrderItemByUserID").Order.Info) ?? new OrderItemResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "根据用户ID查询，获取用户是否有投资过新手标GetUserNoviceOrderItemByUserID(int UserID)", ex.Message.ToString(), UserID);
                return new OrderItemResponse();
            }
            finally
            {
                orderServiceClient.Close();
            }
        }

        #region 获取子订单列表
        /// <summary>
        /// 根据ProductCode获取子订单列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public OrderItemResponse GetOrderItemsByProductCode(string productCode)
        {
            OrderService.OrderServiceClient orderServiceClient = new OrderServiceClient();
            try
            {
                CacheKey cacheKey = CacheItem.InitByIdentity(productCode, "GetOrderItemsByProductCode").OrderItem.Info;
                Func<OrderItemResponse> method = delegate
                {
                    OrderItemResponse res = orderServiceClient.GetOrderItemsByProductCode(productCode);
                    return res.Code == 1 ? res : null;
                };
                return base.GetData<OrderItemResponse>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.GetOrderItemsByProductCode 出现异常", ex.Message.ToString(), productCode);
                return null;
            }
            finally
            {
                orderServiceClient.Close();
            }
        }

        /// <summary>
        /// 根据产品属性获取获取订单
        /// </summary>
        /// <param name="userOrderItemRequest"></param>
        /// <returns></returns>
        public OrderItemResponse GetProperySubOrderItem(UserOrderItemRequest userOrderItemRequest)
        {
            OrderServiceClient orderServiceClient = new OrderServiceClient();
            try
            {
                Func<OrderItemResponse> method = delegate
                    {
                        OrderItemResponse response = orderServiceClient.GetProperySubOrderItem(userOrderItemRequest);
                        return response.Code == 2 ? null : response;
                    };
                return GetData<OrderItemResponse>(method,
                    CacheItem.InitByIdentity(userOrderItemRequest.UserID.ToString()
                    , userOrderItemRequest.PropertySubID
                    , string.Join(",", userOrderItemRequest.OrderItemStatusIDs)).OrderItem.Info) ?? new OrderItemResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "根据产品属性获取获取订单 GetProperySubOrderItem", ex.Message.ToString(), userOrderItemRequest);
                return new OrderItemResponse();
            }
            finally
            {
                orderServiceClient.Close();
            }
        }

        /// <summary>
        /// 根据ProductCode获取子订单列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public OrderItemResponse GetOrderItemsPage(OrderItemRequest request)
        {
            OrderService.OrderServiceClient orderServiceClient = new OrderServiceClient();
            try
            {
                CacheKey cacheKey = CacheItem.InitByIdentity(request.OrderItemDto.ProductCode, "GetOrderItemsPage", request.PageStart, request.PageEnd).OrderItem.Info;
                Func<OrderItemResponse> method = delegate
                {
                    OrderItemResponse res = orderServiceClient.GetOrderItemsPage(request);
                    return res;
                };
                return base.GetData<OrderItemResponse>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.GetOrderItemsPage 出现异常", ex.Message.ToString(), request.OrderItemDto.ProductCode);
                return null;
            }
            finally
            {
                orderServiceClient.Close();
            }
        }

        #endregion

        #region  获取用户投资记录
        /// <summary>
        /// 获取用户投资记录
        /// </summary>
        /// <returns></returns>
        public UserInvestmentResponse GetInvestmentDTOList(UserInvestmentRequest userInvestmentRequest)
        {
            OrderService.OrderServiceClient orderServiceClient = new OrderServiceClient();
            try
            {
                List<object> keylist = new List<object>();
                keylist.Add("GetInvestmentDTOList");
                keylist.Add(userInvestmentRequest.PageIndex);
                keylist.Add(userInvestmentRequest.PageSize);
                keylist.Add(userInvestmentRequest.OrderItemID);
                if (userInvestmentRequest.CategoryIDs != null && userInvestmentRequest.CategoryIDs.Count > 0)
                {
                    keylist.Add(string.Join(",", userInvestmentRequest.CategoryIDs));
                }
                if (userInvestmentRequest.ProductGroupIDs != null && userInvestmentRequest.ProductGroupIDs.Count > 0)
                {
                    keylist.Add(string.Join(",", userInvestmentRequest.ProductGroupIDs));
                }
                if (userInvestmentRequest.ProductStatusIDs != null && userInvestmentRequest.ProductStatusIDs.Count > 0)
                {
                    keylist.Add(string.Join(",", userInvestmentRequest.ProductStatusIDs));
                }
                CacheKey cacheKey = CacheItem.InitByIdentity(userInvestmentRequest.UserID + "", keylist.ToArray()).Order.Info;
                Func<UserInvestmentResponse> method = delegate
                {
                    UserInvestmentResponse userinvestRes = orderServiceClient.GetInvestmentDTOList(userInvestmentRequest);
                    return userinvestRes.Code == 1 ? userinvestRes : null;
                };
                return base.GetData<UserInvestmentResponse>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.GetInvestmentDTOList 出现异常", ex.Message.ToString(), userInvestmentRequest);
                return null;
            }
            finally
            {
                orderServiceClient.Close();
            }
        }


        #endregion

        #region 用户体验金使用接口

        /// <summary>
        /// 获取用户体验金订单
        /// </summary>
        /// <returns></returns>
        public OrderItemResponse GetUserExperiencerOrderItem(UserOrderItemRequest userOrderItemRequest)
        {
            OrderServiceClient service = new OrderServiceClient();
            try
            {
                Func<OrderItemResponse> method = delegate
                    {
                        OrderItemResponse response = service.GetUserExperiencerOrderItem(userOrderItemRequest);
                        return response.Code != 1 ? null : response;
                    };
                return GetData<OrderItemResponse>(method
                    , CacheItem.InitByIdentity("UserExperiencerOrderItem"
                    , userOrderItemRequest.UserID).OrderItem.Info) ?? new OrderItemResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.Order.GetUserExperiencerOrderItem 出现异常", ex.Message, userOrderItemRequest);
                return new OrderItemResponse();
            }
            finally
            {
                service.Close();
            }
        }

        #endregion

        /// <summary>
        /// 创建产品订单
        /// </summary>
        /// <param name="tenderRequest"></param>
        /// <param name="isVirtualUser"></param>
        /// <returns></returns>
        public InitiativeTenderResponse CreateOrderItem(InitiativeTenderRequest tenderRequest)
        {
            OrderServiceClient service = new OrderServiceClient();
            try
            {
                return service.CreateOrderItem(tenderRequest);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.Order.GetUserExperiencerOrderItem 出现异常", ex.Message, tenderRequest);
                return null;
            }
            finally
            {
                service.Close();
            }
        }

        /// <summary>
        /// 根据订单ID获取订单信息
        /// </summary>
        /// <param name="orderItemId"></param>
        /// <returns></returns>
        public OrderItemDTO GetOrderItemById(int orderItemId)
        {
            OrderServiceClient client = new OrderServiceClient();
            try
            {
                CacheKey cacheKey = CacheItem.InitByIdentity("GetOrderItemById", orderItemId).OrderItem.Info;
                Func<OrderItemDTO> method = delegate
                {
                    OrderItemDTO res = client.GetOrderItemById(orderItemId);
                    return res;
                };
                return base.GetData<OrderItemDTO>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取订单信息GetOrderItemById 出现异常", ex.Message.ToString(), orderItemId);
                return null;
            }
            finally
            {
                client.Close();
            }
        }

        /// <summary>
        /// 查询活期订单
        /// Date:2018-5-16 11:33:26
        /// Author:Linsan
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public UserInvestmentResponse GetCurrentInvestmentDtoList(UserInvestmentRequest request)
        {
            OrderServiceClient client = new OrderServiceClient();
            try
            {
                List<object> keylist = new List<object>();
                keylist.Add("GetCurrentInvestmentDtoList");
                if (request.PageIndex > 0)
                {
                    keylist.Add(request.PageIndex);
                    keylist.Add(request.PageSize);
                }
                if (request.PaymentTime.Year > 2000)
                {
                    keylist.Add(request.PaymentTime.ToString("yyyyMMddHHssmm"));
                }
                CacheKey cacheKey = CacheItem.InitByIdentity(request.UserID + "", keylist.ToArray()).OrderItem.Info;
                Func<UserInvestmentResponse> method = delegate
                {
                    UserInvestmentResponse userinvestRes = client.GetCurrentInvestmentDtoList(request);
                    return userinvestRes.Code == 1 ? userinvestRes : null;
                };
                return base.GetData<UserInvestmentResponse>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "查询活期订单GetCurrentInvestmentDtoList 出现异常", ex.Message.ToString(), request.UserID);
                return null;
            }
            finally
            {
                client.Close();
            }
        }

        /// <summary>
        /// 获取活期订单交易金额
        /// </summary>
        /// <returns></returns>
        public OrderItemTransferResponse GetOrderItemTransferPrice(OrderItemTransferRequest request)
        {
            OrderServiceClient client = new OrderServiceClient();
            try
            {
                List<object> keylist = new List<object>();
                keylist.Add("GetOrderItemTransferPrice");
                if (request.OrderItemTransferTypeIDs.Count > 0)
                {
                    keylist.Add(string.Join(",", request.OrderItemTransferTypeIDs));
                }
                if (request.OrderItemTransferStatusIDs.Count > 0)
                {
                    keylist.Add(string.Join(",", request.OrderItemTransferStatusIDs));
                }
                if (request.PaymentTime.Year > 2000)
                {
                    keylist.Add(request.PaymentTime.ToString("yyyyMMddHHssmm"));
                }
                CacheKey cacheKey = CacheItem.InitByIdentity(request.UserId + "", keylist.ToArray()).Order.Info;
                Func<OrderItemTransferResponse> method = delegate
                {
                    OrderItemTransferResponse userinvestRes = client.GetOrderItemTransferPrice(request);
                    return userinvestRes.Code == 1 ? userinvestRes : null;
                };
                return base.GetData<OrderItemTransferResponse>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取活期订单交易金额GetOrderItemTransferPrice 出现异常", ex.Message, request.UserId);
                return null;
            }
            finally
            {
                client.Close();
            }
        } 
    }
}
